﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Media.Imaging;
using ImageTools.IO.Gif;
using ImageTools.Controls;
using System.IO;
using System.Text.RegularExpressions;

namespace WeiTalk.Controls
{
    public partial class PictureViewerControl : UserControl
    {
        FrameworkElement imageControl = null;

        public Stream ImageSource
        {
            get;
            set;
        }

        public bool IsGif
        {
            get;
            set;
        }

        private double ImageActuallyWidth
        {
            get
            {
                if (imageControl is Image)
                {
                    BitmapImage bt = ((Image)imageControl).Source as BitmapImage;
                    if (bt != null)
                    {
                        return bt.PixelWidth;
                    }
                }
                else if (imageControl is AnimatedImage)
                {
                    return ((AnimatedImage)imageControl).Source.PixelWidth;
                }
                return 0;
            }
        }

        private double ImageActuallyHeight
        {
            get
            {
                if (imageControl is Image)
                {
                    BitmapImage bt = ((Image)imageControl).Source as BitmapImage;
                    if (bt != null)
                    {
                        return bt.PixelHeight;
                    }
                }
                else if (imageControl is AnimatedImage)
                {
                    return ((AnimatedImage)imageControl).Source.PixelHeight;
                }
                return 0;
            }
        }

        public PictureViewerControl()
        {
            InitializeComponent();
            
        }
        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            CreateWeiBoImageControl(Guid.NewGuid().ToString(), ImageSource, spImageViewer, IsGif);

            double imgHeight = ImageActuallyHeight;

            if (ImageActuallyWidth > imageControl.ActualWidth)
            {
                if (imageControl is AnimatedImage)
                {
                    ((AnimatedImage)imageControl).Stretch = Stretch.Uniform;
                }
                else if (imageControl is Image)
                {
                    ((Image)imageControl).Stretch = Stretch.Uniform;
                }

            }
            else if (imgHeight > imageControl.ActualHeight)
            {
                imageControl.Height = ImageActuallyHeight;
            }
        }

        private UIElement CreateWeiBoImageControl(string name, Stream s, StackPanel parentControl, bool isGif)
        {
            if (isGif)
            {
                imageControl = new AnimatedImage
                {
                    Name = name,
                    Margin = new Thickness { Left = 5 },
                    Stretch = Stretch.Uniform
                };
                parentControl.Children.Add(imageControl);
                ImageTools.IO.Decoders.AddDecoder<GifDecoder>();
                ImageTools.ExtendedImage image = new ImageTools.ExtendedImage();
                GifDecoder decoder = new GifDecoder();
                decoder.Decode(image, s);
                ((AnimatedImage)imageControl).Source = image;
            }
            else
            {
                imageControl = new Image
                {
                    Name = name,
                    Margin = new Thickness { Left = 5 },
                    Stretch = Stretch.Uniform
                };
                parentControl.Children.Add(imageControl);

                BitmapImage bi = new BitmapImage();
                bi.SetSource(s);
                bi.CreateOptions = BitmapCreateOptions.BackgroundCreation;
                ((Image)imageControl).Source = bi;
            }
            return imageControl;
        }
    }
}
